#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;
const int M = 5e4 + 5;
const int MOD = 10000;

int n, m, s, t, t0;
int head[N], deg[N];
int ver[M], W[M], Next[M], tot;
void add(int u, int v, int w) {
  ++tot;
  ver[tot] = v;
  W[tot] = w;
  Next[tot] = head[u];
  head[u] = tot;
}
struct Node {
  int dis, cnt;
} ans[N];
int toposrot() {
  ans[s] = Node{0, 1};
  queue<int> q;
  rep(i, 1, n) if (!deg[i]) q.push(i);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = Next[i]) {
      int v = ver[i];
      int w = W[i];
      int x = (ans[u].dis + ans[u].cnt * w) % MOD;
      ans[v].dis = (ans[v].dis + x) % MOD;
      ans[v].cnt = (ans[v].cnt + ans[u].cnt) % MOD;
      if (--deg[v] == 0) q.push(v);
    }
  }
  return (ans[t].dis + (ans[t].cnt + MOD - 1) * t0) % MOD;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> s >> t >> t0;
  rep(i, 1, m) {
    int u, v, w;
    cin >> u >> v >> w;
    add(u, v, w);
    deg[v]++;
  }
  cout << toposrot();
  return 0;
}